﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.ModelInterface;
using QLBH.Category;
using QLBH.DataModel;
using QLBH.StandardUI;
using QLBH.ModelFactory;

namespace QLBH.CongNo
{
    public partial class PhieuThuChiDlg : Form
    {
        private eDlgType dlgType;

        public eDlgType DlgType
        {
            get 
            { 
                return dlgType; 
            }
            set 
            { 
                dlgType = value;                
            }
        }

        private IPhieuThuChiModel model;

        public IPhieuThuChiModel Model
        {
            get { return model; }
            set
            {
                model = value;

                if (dlgType == eDlgType.EDIT)
                {
                    InitControlValue();
                }
            }
        }

        private void InitControlValue()
        {
            tbxSoPhieu.Text = model.SOPHIEU;
            tbxNgayTao.Text = model.NGAYTAO.ToShortDateString();
            tbxNguoiTao.Text = model.USERNAME;
            tbxsoTien.Text = model.TONGTIEN.ToString();

            
           
            rtbGhiChu.Text = model.GHICHU;
        }        
        public PhieuThuChiDlg()
        {
            InitializeComponent();
            model = CongNoModelFactory.GetPhieuThuChiModelInstance();
            model.NGAYTAO = DateTime.Now;
        }
        public PhieuThuChiDlg(bool loaiPhieu)
        {
            InitializeComponent();
            model = CongNoModelFactory.GetPhieuThuChiModelInstance();
            model.NGAYTAO = DateTime.Now;
            model.LOAIPHIEU = loaiPhieu;
        }

        private void LoadDSLyDo()
        {
            try
            {
                List<LYDO> lydos = LyDoController.GetInstance().GetAll().ToList();
                cmbLyDo.DataSource = lydos;
                cmbLyDo.DisplayMember = "TEN";
                cmbLyDo.ValueMember = "ID";
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }            
        }

        private void lbAddLyDo_Click(object sender, EventArgs e)
        {            
            AddLyDoDlg addNewLyDoDlg = new AddLyDoDlg();
            if (addNewLyDoDlg.ShowDialog()==DialogResult.OK)
            {
                cmbLyDo.DataSource=null;
                LoadDSLyDo();
                cmbLyDo.SelectedValue = addNewLyDoDlg.Model.ID;
            }
        }

        private void PhieuThuChiDlg_Load(object sender, EventArgs e)
        {
            LoadDSLyDo();
            if (dlgType==eDlgType.ADD)
            {
                tbxNgayTao.Text = DateTime.Now.ToShortDateString();
                tbxNguoiTao.Text = AccountHandler.GetCurrentUserLogin().TENNV;

                tbxSoPhieu.Text = CodeHelper.GetNewCode();
            }
            else
            {
                cmbLyDo.SelectedValue = model.MALYDO;
                tbxNgayTao.Text = model.NGAYTAO.ToShortDateString();
                tbxNguoiTao.Text = model.USERNAME;
                tbxSoPhieu.Text = model.SOPHIEU;
            }            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Check valid control value and update value from control to model                        
                if (CheckvalidValue())
                {
                    model.SOPHIEU = tbxSoPhieu.Text;
                    model.USERID = AccountHandler.GetCurrentUserLogin().NVID;
                    model.USERNAME = AccountHandler.GetCurrentUserLogin().TENNV;
                    model.TONGTIEN = decimal.Parse(tbxsoTien.Text);
                    model.MALYDO = (cmbLyDo.SelectedItem as LYDO).ID;
                    model.TENLYDO = (cmbLyDo.SelectedItem as LYDO).TEN;
                    model.GHICHU = rtbGhiChu.Text;                    

                    if (eDlgType.ADD == dlgType)
                    {
                        //Add new
                        model.MAPHIEU = Guid.NewGuid();

                        PhieuThuChiController.GetInstance().Add(model.ToEntity());
                    }
                    else
                    {
                        //edit
                        PhieuThuChiController.GetInstance().Edit(model.ToEntity());
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }            
        }

        private bool CheckvalidValue()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxSoPhieu.Text, "Số phiếu", 14);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxSoPhieu.Focus();
                tbxSoPhieu.SelectAll();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(tbxsoTien.Text, "Số tiền");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxsoTien.Focus();
                tbxsoTien.SelectAll();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextDataLength(rtbGhiChu.Text, "Ghi chú", 200);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                rtbGhiChu.Focus();
                rtbGhiChu.SelectAll();
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void PhieuThuChiDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Escape)
            {
                btnCancel_Click(btnCancel, null);
            }
            else if (e.KeyCode==Keys.S && e.Control)
            {
                btnSave_Click(btnSave, null);
            } 
            else if (e.KeyCode==Keys.N && e.Control)
            {
                lbAddLyDo_Click(lbAddLyDo, null);
            }
        }
    }
}
